// app.js
App({
  onLaunch() {

  },
  globalData: {   
  },
  /**
   * 保存缓存
   * minute:缓存有效期分钟数
   */
  saveCache(key, value, minute) {
    wx.setStorageSync(key, value);
    let ms = new Date();
    ms = ms.getTime() + minute * 60 * 1000;
    wx.setStorageSync(key + '_time', ms); //有效期
  },
  //获取缓存
  getCache(key) {
    //检查是否过期
    let ms = wx.getStorageSync(key + '_time') || 0;
    let d = new Date();
    let ms1 = d.getTime();
    if (ms > ms1) { //未过期，正常取值
      return wx.getStorageSync(key);
    } else { //已过期，清除相关缓存
      console.log(key + ' 缓存不存在或已过期');
      this.delCache(key);
      return;
    }
  },
  /**
   * 删除缓存
   */
  delCache(key) {
    wx.removeStorageSync(key);
    wx.removeStorageSync(key + '_time');
  },
    /**
   * 批量删除缓存
   * @param {*} arrys 为数组。从所有本地缓存的key中匹配arrys中的值
   */
  delMoreCache(arrys) {
    let res = wx.getStorageInfoSync(); //所有本地缓存
    let keys = res.keys; //本地缓存中key集合
    //console.log('本地所有缓存：', keys);
    //console.log('需匹配的值：', arrys);

    let cacheKeys = []; //结果集
    keys.forEach(item => {
      for (let i = 0; i < arrys.length; i++) {
        if (item.indexOf(arrys[i]) != -1) {
          cacheKeys.push(item);
          break;
        }
      }
    })
    //console.log('结果key:', cacheKeys);
    cacheKeys.forEach(item => {
      this.delCache(item);
    })
  },
})
